package com.miyake.loveviewweb.client.community;

import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.miyake.loveviewweb.client.dialog.BasicDialog;
import com.miyake.loveviewweb.client.service.DataService;
import com.miyake.loveviewweb.client.viewer.AbstractListWidget;
import com.miyake.loveviewweb.shared.model.UserModel;

public class FriendListWidget extends AbstractListWidget<UserModel> {
    private FriendListType type;
    
    protected void updateSuper() {
        super.update();
    }
    
    @Override
    public void update() {
        if (type.equals(FriendListType.MyFriends)) {
            DataService.Util.getInstance().getMyFriends(new AsyncCallback<List<UserModel>>() {
                public void onFailure(Throwable caught) {
                }

                public void onSuccess(List<UserModel> result) {
                    setElements(result);
                    updateSuper();
                }
            });
        }
        else if (type.equals(FriendListType.AllUsers)) {
            DataService.Util.getInstance().getAllUsers(new AsyncCallback<List<UserModel>>() {

                public void onFailure(Throwable caught) {
                    // TODO Auto-generated method stub
                    
                }

                public void onSuccess(List<UserModel> result) {
                    setElements(result);
                    updateSuper();
                }
                
            });
        }
    }
    
    public enum FriendListType {
        MyFriends, AllUsers
    }
    public FriendListWidget(FriendListType type) {
        this.type = type;
        getGrid().setBorderWidth(1);
        setColumns(6);
        update();
    }
    @Override
    protected Widget getWidget(final UserModel element) {
        VerticalPanel panel = new VerticalPanel();
        
        Image image = new Image();
        image.setSize("100px", "100px");
        image.setUrl(element.getImageUrl());
        panel.add(image);

        Anchor name = new Anchor(element.getFirstName() + "," + element.getLastName());
        panel.add(name);
        ClickHandler clickHandler = new ClickHandler() {
            public void onClick(ClickEvent event) {
                onSelect(element);
            }
        };
        
        name.addClickHandler(clickHandler);
        image.addClickHandler(clickHandler);
        
        return panel;
    }

    protected void showProfileDialog(UserModel element) {
        new BasicDialog(new UserPropertyPanel(element, false)) {

            @Override
            protected void onOK(Composite composite) {

            }
            
        }.showDialog();
    }

    protected void onSelect(final UserModel element) {
        showProfileDialog(element);
    }
    

}
